VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
END
Attribute VB_Name = "Products"
Attribute VB_Creatable = True
Attribute VB_Exposed = True
Function CreateResponse(request As String) As String
    Dim HTMLResponse As String
    
    WriteHeader HTMLResponse
    WriteOrderConfirmTable request, HTMLResponse
    WriteFooter HTMLResponse
    
    CreateResponse = HTMLResponse
End Function
Sub WriteHeader(HTMLResponse As String)
    HTMLResponse = "Content-Type: text/html" & vbCrLf & vbCrLf _
        & "<HTML><BODY><h3>Thank you for your order: </h3>"
End Sub
Sub WriteFooter(HTMLResponse As String)
    HTMLResponse = HTMLResponse & "</BODY> </HTML>"
End Sub

Sub WriteOrderConfirmTable(request As String, HTMLResponse As String)
    Dim sSubTotal As Single
    Dim sTax As Single
    Dim NameValue As String
    Dim iThisAnd As Integer, iNextAnd As Integer
    Dim iEqualPos As Integer
    Dim sql As String
    Dim db As Database
    Dim rs As Recordset
    
    WriteTableHeadings HTMLResponse
    
    'open NorthWind database...need to look up item names and prices
    'use location on Internet server
    Set db = OpenDatabase("c:\MID\Labs\MainSt.mdb")
    
    'parse request looking for items that are being ordered: productID=quantity
    iThisAnd = 1
    iNextAnd = InStr(request, "&")
    'the last entry in the param list is the button
    'used to invoke this OLE server...so we don't need it
    Do Until iNextAnd = 0
        NameValue = Mid(request, iThisAnd, iNextAnd - iThisAnd)
        iEqualPos = InStr(NameValue, "=")
        Name = Left(NameValue, iEqualPos - 1)
        Value = Right(NameValue, Len(NameValue) - iEqualPos)
        
        'add item to summary table if a quantity was entered
        If Value <> "0" Then
            'get item information out of database
            sql = "Select productName, unitprice from products where productID = " & Name
            Set rs = db.OpenRecordset(sql, dbOpenSnapshot)
            
            'create table of ordered items
            'TODO: unitprice needs to be formatted
            HTMLResponse = HTMLResponse & _
                "<TR><TD>" & Value & _
                "</TD><TD>" & rs.Fields("ProductName") & _
                "</TD><TD>" & rs.Fields("Unitprice") & _
                "</TD></TR>"
        
            'keep track of subtotal
            sSubTotal = sSubTotal + (Value * rs.Fields("Unitprice"))
            
            'close the recordset
            rs.Close
        End If
        
        'look for next &
        iThisAnd = iNextAnd + 1
        iNextAnd = InStr(iThisAnd, request, "&")
    Loop
    sTax = sSubTotal * 0.0825
    
    'add subtotal, tax, total
    'TODO: format Sub, Tax and Total as currency
    HTMLResponse = HTMLResponse & _
        "<TR><TD colspan=2 align=right>Sub Total: </TD><TD>" & _
        sSubTotal & "</TD></TR>" & _
        "<TD colspan=2 align=right>Tax: </TD><TD>" & _
        sTax & "</TD></TR>" & _
        "<TD colspan=2 align=right>Total: </TD><TD>" & _
        sSubTotal + sTax & "</TD></TR>"
                
    'end table
    HTMLResponse = HTMLResponse & "</TABLE>"
    
    'close the database
    db.Close

End Sub

Sub Post(request As String, response As String)
    response = CreateResponse(request)
End Sub


Sub WriteTableHeadings(HTML As String)
    HTML = HTML & "<Table border=1 rules=rows> "
End Sub



